package com.bejoy.coolatin;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.os.Bundle;
import android.text.style.BulletSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BaiduMap.OnMarkerClickListener;
import com.baidu.mapapi.map.BaiduMap.OnMarkerDragListener;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.GroundOverlayOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.InfoWindow.OnInfoWindowClickListener;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.overlayutil.TransitRouteOverlay;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;

/**
 * 演示覆盖物的用法
 */
public class MapTestActivity extends Activity {

	/**
	 * MapView 是地图主控件
	 */
	public View popView;
	private MapView mMapView;
	private BaiduMap mBaiduMap;
	private Marker mMarkerA;
	private Marker mMarkerB;
	private Marker mMarkerC;
	private Marker mMarkerD;
	private InfoWindow mInfoWindow;
	RoutePlanSearch mSearch = null;
	// 初始化全局 bitmap 信息，不用时及时 recycle
	BitmapDescriptor bdA = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markb);
	BitmapDescriptor bdB = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markb);
	BitmapDescriptor bdC = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markc);
	BitmapDescriptor bdD = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markd);
	BitmapDescriptor bd = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markd);
	BitmapDescriptor bdGround = BitmapDescriptorFactory
			.fromResource(R.drawable.icon_markd);
	String[] asdStrings = { "123" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_overlay);
		mMapView = (MapView) findViewById(R.id.bmapView);
		mBaiduMap = mMapView.getMap();
		MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(14.0f);
		mBaiduMap.setMapStatus(msu);
		initOverlay();
		mSearch = RoutePlanSearch.newInstance();

		mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

			@Override
			public boolean onMarkerClick(final Marker marker) {
				// marker.setIcon(bdB);
				// GeoPoint pt = mGeoList.get(i).getPoint();
				// new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
				// LayoutParams.WRAP_CONTENT, pt,
				// MapView.LayoutParams.BOTTOM_CENTER));
				System.out.println("!===" + mBaiduMap.getMapType());

				Button button = new Button(getApplicationContext());
				button.setBackgroundResource(R.drawable.popup);
				OnInfoWindowClickListener listener = null;
					button.setText("啦啦啦啦啦");
					listener = new OnInfoWindowClickListener() {
						public void onInfoWindowClick() {
 //							LatLng llNew = new LatLng(ll.latitude + 0.005,
//									ll.longitude + 0.005);
//							marker.setPosition(llNew);
							mBaiduMap.hideInfoWindow();
						}
					};
					LatLng ll = marker.getPosition();
					mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), ll, -47, listener);
					mBaiduMap.showInfoWindow(mInfoWindow);
					
				AlertDialog.Builder builder = new AlertDialog.Builder(
						MapTestActivity.this).setPositiveButton("导航",
						new OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								LatLng llD = new LatLng(39.93, 116.7);
								PlanNode stNode = PlanNode
										.withCityNameAndPlaceName("北京",
												"环铁一线艺术区");
								PlanNode enNode = PlanNode.withLocation(llD);
								mSearch.transitSearch((new TransitRoutePlanOption())
										.from(stNode).city("北京").to(enNode));
								dialog.dismiss();
								mSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {

									@Override
									public void onGetWalkingRouteResult(
											WalkingRouteResult arg0) {
										// TODO Auto-generated method stub

									}

									@Override
									public void onGetTransitRouteResult(
											TransitRouteResult result) {
										if (result == null
												|| result.error != SearchResult.ERRORNO.NO_ERROR) {
											Toast.makeText(MapTestActivity.this,
													"抱歉，未找到结果",
													Toast.LENGTH_SHORT).show();
										}
										if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
											// 起终点或途经点地址有岐义，通过以下接口获取建议查询信息
											// result.getSuggestAddrInfo()
											return;
										}
										if (result.error == SearchResult.ERRORNO.NO_ERROR) {
											TransitRouteOverlay overlay1 = new MyTransitRouteOverlays(
													mBaiduMap);
											overlay1.removeFromMap();
											mBaiduMap.clear();
											mBaiduMap
													.setOnMarkerClickListener(overlay1);
											// result.getRouteLines().get(0).getAllStep().size()
											// for (int i = 0; i <6; i++) {
											// asdStrings[i]=
											// result.getRouteLines()
											// .get(0)
											// .getAllStep()
											// .get(i)
											// .getInstructions()
											// .toString();
											// System.out.println("!===asdString"+asdStrings[i]);
											// asdStrings[i] = result
											// .getRouteLines().get(0)
											// .getAllStep().get(i)
											// .getInstructions()
											// .toString();
											// asdStrings[i]=result.getRouteLines().get(0).getAllStep().get(i).getInstructions().toString();
											// }

											overlay1.setData(result
													.getRouteLines().get(0));
											overlay1.addToMap();
											overlay1.zoomToSpan();
											// route =
											// result.getRouteLines().get(0);
											// List<DrivingStep>
											// step=route.getAllStep();
											// String nodeTitle = null;
											// nodeTitle=step.get(0).getInstructions();
											// System.out.println("!===1231232131312"
											// + step);
										}
									}

									@Override
									public void onGetDrivingRouteResult(
											DrivingRouteResult arg0) {
										// TODO Auto-generated method stub

									}
								});
							}
						}).setNegativeButton("详细信息", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// startActivity(new Intent(OverlayDemo.this,
						// Demoactivity.class).putExtra("信息", asdStrings));
						dialog.dismiss();
					}
				});
				builder.create().show();
				return false;

			}
		});

		// mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
		// public boolean onMarkerClick(final Marker marker) {
		// Button button = new Button(getApplicationContext());
		// button.setBackgroundResource(R.drawable.popup);
		// OnInfoWindowClickListener listener = null;
		// if (marker == mMarkerA || marker == mMarkerD) {
		// button.setText("更改位置");
		// listener = new OnInfoWindowClickListener() {
		// public void onInfoWindowClick() {
		// LatLng ll = marker.getPosition();
		// LatLng llNew = new LatLng(ll.latitude + 0.005,
		// ll.longitude + 0.005);
		// marker.setPosition(llNew);
		// mBaiduMap.hideInfoWindow();
		// }
		// };
		// LatLng ll = marker.getPosition();
		// mInfoWindow = new InfoWindow(BitmapDescriptorFactory
		// .fromView(button), ll, -47, listener);
		// mBaiduMap.showInfoWindow(mInfoWindow);
		// } else if (marker == mMarkerB) {
		// button.setText("更改图标");
		// button.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) {
		// marker.setIcon(bd);
		// mBaiduMap.hideInfoWindow();
		// }
		// });
		// LatLng ll = marker.getPosition();
		// mInfoWindow = new InfoWindow(button, ll, .-47);
		// mBaiduMap.showInfoWindow(mInfoWindow);
		// } else if (marker == mMarkerC) {
		// button.setText("删除");
		// button.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) {
		// marker.remove();
		// mBaiduMap.hideInfoWindow();
		// }
		// });
		// LatLng ll = marker.getPosition();
		// mInfoWindow = new InfoWindow(button, ll, -47);
		// mBaiduMap.showInfoWindow(mInfoWindow);
		// }
		// return true;
		// }
		// });
	}

	public void initOverlay() {
		// add marker overlay
		LatLng llA = new LatLng(39.9, 116.4);
		LatLng llB = new LatLng(39.91, 116.5);
		LatLng llC = new LatLng(39.92, 116.6);
		LatLng llD = new LatLng(39.93, 116.7);

		OverlayOptions ooA = new MarkerOptions().position(llA).icon(bd)
				.zIndex(9).draggable(true);
		mMarkerA = (Marker) (mBaiduMap.addOverlay(ooA));
		OverlayOptions ooB = new MarkerOptions().position(llB).icon(bd)
				.zIndex(5);
		OverlayOptions ooC = new MarkerOptions().position(llC).icon(bd)
				.zIndex(5);
		OverlayOptions ooD = new MarkerOptions().position(llD).icon(bd)
				.zIndex(5);
		mMarkerB = (Marker) (mBaiduMap.addOverlay(ooB));
		mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));
		mMarkerD = (Marker) (mBaiduMap.addOverlay(ooD));
		// OverlayOptions ooC = new MarkerOptions().position(llC).icon(bd)
		// .perspective(false).anchor(0.5f, 0.5f).rotate(30).zIndex(7);
		// mMarkerC = (Marker) (mBaiduMap.addOverlay(ooC));
		// ArrayList<BitmapDescriptor> giflist = new
		// ArrayList<BitmapDescriptor>();
		// giflist.add(bdA);
		// giflist.add(bdB);
		// giflist.add(bdC);
		// OverlayOptions ooD = new MarkerOptions().position(llD).icons(giflist)
		// .zIndex(0).period(10);

		// add ground overlay
		// LatLng southwest = new LatLng(39.92235, 116.380338);
		// LatLng northeast = new LatLng(39.947246, 116.414977);
		// LatLngBounds bounds = new LatLngBounds.Builder().include(northeast)
		// .include(southwest).build();
		//
		// OverlayOptions ooGround = new GroundOverlayOptions()
		// .positionFromBounds(bounds).image(bdGround).transparency(0.8f);
		// mBaiduMap.addOverlay(ooGround);

		// MapStatusUpdate u = MapStatusUpdateFactory
		// .newLatLng(bounds.getCenter());
		// mBaiduMap.setMapStatus(u);

		mBaiduMap.setOnMarkerDragListener(new OnMarkerDragListener() {
			public void onMarkerDrag(Marker marker) {
			}

			public void onMarkerDragEnd(Marker marker) {
				Toast.makeText(
						MapTestActivity.this,
						"拖拽结束，新位置：" + marker.getPosition().latitude + ", "
								+ marker.getPosition().longitude,
						Toast.LENGTH_LONG).show();
			}

			public void onMarkerDragStart(Marker marker) {
			}
		});
	}

	/**
	 * 清除所有Overlay
	 * 
	 * @param view
	 */
	public void clearOverlay(View view) {
		mBaiduMap.clear();
	}

	/**
	 * 重新添加Overlay
	 * 
	 * @param view
	 */
	public void resetOverlay(View view) {
		clearOverlay(null);
		initOverlay();
	}

	@Override
	protected void onPause() {
		// MapView的生命周期与Activity同步，当activity挂起时需调用MapView.onPause()
		mMapView.onPause();
		super.onPause();
	}

	@Override
	protected void onResume() {
		// MapView的生命周期与Activity同步，当activity恢复时需调用MapView.onResume()
		mMapView.onResume();
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// MapView的生命周期与Activity同步，当activity销毁时需调用MapView.destroy()
		mMapView.onDestroy();
		super.onDestroy();
		// 回收 bitmap 资源
		bdA.recycle();
		bdB.recycle();
		bdC.recycle();
		bdD.recycle();
		bd.recycle();
		bdGround.recycle();
	}

	private class MyTransitRouteOverlays extends TransitRouteOverlay {

		public MyTransitRouteOverlays(BaiduMap baiduMap) {
			super(baiduMap);
			mBaiduMap.clear();
		}

		@Override
		public BitmapDescriptor getStartMarker() {
			return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
		}

		@Override
		public BitmapDescriptor getTerminalMarker() {
			return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
		}
	}

}
